<template>
	<div v-if="!IsTechnician">
		<button class="bg-tp bd-solid btn fs-26" v-if="info.State === 0" @click.stop="cancle(info)">取消订单</button>
		<button class="bg-physio-primary1 color-white btn fs-26" v-if="info.State === 0" @click.stop="goPay(info)">立即支付</button>
		<button class="bg-physio-primary1 color-white btn fs-26" @click.stop="openRefundApply(info)" v-if="switch30 === 1 && (info.State === 1 ||(info.State === 2 && refund))">申请退款</button>
		<button class="bg-tp bd-solid btn fs-26" v-if="info.State === 4" @click.stop="evaluate(info)">立即评价</button>
		<button class="bg-tp bd-solid btn fs-26" v-if="info.State === 4 || info.State === 5" @click.stop="againOrder(info,0)">再次预约</button>
	</div>
	<div v-else>
		<button class="bg-physio-primary1 color-white  btn fs-26" v-if="info.State === 2" @click.stop="confirmArrive(info)">确认到达</button>
		<button class="bg-physio-primary1 color-white btn fs-26" @click.stop="setOut(info)" v-if="info.State === 1 && info.State !== 7">开始接单</button>
		<button class="bg-physio-primary1 color-white btn fs-26" @click.stop="startService(info)" v-if="info.State === 8">开始服务</button>
		<button class="bg-physio-primary1 color-white  btn fs-26" v-if="info.State === 3" @click.stop="writeOff(info)">核销</button>
	</div>
</template>
<script>
import { BookHomeOrderConfirmGo,BookHomeOrderConfirmArrived} from 'api/physio'
import { CancleOrder } from 'api/teaHouse'
export default {
	props:{
		info: {
      type: Object,
      default() {
        return {}
      }
    },
		switch30: {
			type: Number,
			default: 1
		},
		technician: {
			type: Boolean,
			default: false
		},
	},
	data(){
		return {
			IsTechnician: false, //是否技师端
			recommendCondition: {},
			Lng: 1,
			Lat: 1,
		}
	},
	watch: {
		info(newVal,oldVal){
			this.info = newVal
		},
	},
	created(){
	
	},
	computed: {
		refund(){
			let _refund = false
			const now = new Date()
			const serviceTime =  new Date(this.info.ServiceStartTime)
			var dateDiff = now.getTime() - serviceTime.getTime()
			dateDiff = Math.floor(dateDiff/(1000*60))  //计算相差分钟数
			if(dateDiff > 30){
				_refund = true
			}
			return _refund;
		}
	},
	mounted(){
		if(this.technician){
			this.IsTechnician = true
		}	
	},
	methods:{
		//开始服务
		startService(item){
			this.$emit('startService',item)
		},
		//取消订单
		cancle(item){
			CancleOrder(item.OrderId).then(res => {
				if (res.Data) {
					// 获取列表
					this.$emit("reset")
				}
			})
		},
		// 申请退款
		openRefundApply(item) {
			this.$emit('refund',item)
		},
		//确认出发(技师端)
		setOut(item){
			BookHomeOrderConfirmGo({orderId:item.OrderId}).then(res => {
				if(res.Data){
					this.$emit('reset')
				}
			})
		},   
		// 立即支付
		goPay(item) {
			window.location.href = this.$LocalDomain + '/m/product/PayOrder?id=' + item.OrderId + '&&singleflag=1&&isBookHomeOrder=1'
		},
		//确认到达
		confirmArrive(item){
			this.$nextTick(() =>{
				this.getCurrentPosition()
			})
			const obj = {}
			obj.Lng = this.Lng || 1
			obj.Lat = this.Lat || 1
			obj.orderId = item.OrderId
			BookHomeOrderConfirmArrived(obj).then(res => {
				if(res.State === 1){
					this.$emit('reset')
				}else{
					this.$toast(res.Msg)
				}
			})
		},
		//再次预定
		againOrder(item,type) {
			const detailList = []
			if(item.DetailList.length > 0){
				item.DetailList.forEach(ele => {
					const _obj = {}
					_obj.Amount = ele.Amount
					_obj.ProductId = ele.ProductId
					detailList.push(_obj)
				});
			}
			this.$router.push({
				path: '/physioTechnicianDetail',
				query: {
					userId: item.ArtificerUserId,
					id: item.ArtificerId,
					detailList: detailList,
				}
			})
		},
		//评价
		evaluate(item){
			this.$router.push({
				path: '/physioEvaluation',
				query: {
					item: item
				}
			})
		},
		//核销
		writeOff(){
			this.$router.push('/physioWriteoff')
		},

		// 监听定位
		getCurrentPosition(cityName) {
      const _this = this
      // 对查询条件经纬度赋值0 0
      delete _this.Lng
      delete _this.Lat
    
      _this.$geolocation.getCurrentPosition(function(status, result) {
        if (status === 'complete') {
          _this.onComplete(result)
        } else {
          _this.onError(result)
        }
      })
    },
		 // 精准定位失败
		 onError(data) {
      console.log('精准定位失败')
      const _this = this
      _this.$geolocation.getCityInfo(function(status, result) {
        if (status === 'complete') {
          _this.onComplete1(result)
        } else {
          _this.onError1(result)
        }
      })
    },
    // ip定位成功
    onComplete1(data) {
      console.log('ip定位成功')
      console.log(data,"地区")
			this.recommendCondition.adcode = data.adcode
			this.recommendCondition.selectCityId = 0
      //localStorage.setItem('GeolocationData', JSON.stringify(data))
      this.$toast.clear()
      //this.closeAreaSelect(false)
      // 对查询条件经纬度赋值
      this.Lng = data.center[0]
      this.Lat = data.center[1]
      // 第一次初始化定位城市
      if (!this.locationInit) {
        //this.locationCity.Name = data.city
        const adcode = parseInt(data.adcode.substr(0, 4) + '00').toString()
        //this.GetCityIdByCode(adcode)
      } else {
        this.listLoad = true
        this.isRefresh = true
        this.onRefresh()
      }
    },
    // ip定位失败
    onError1(data) {
      console.log('ip定位失败')
      console.log(data)
      this.$toast.clear()
      this.$toast.fail({
        message: '获取定位信息失败，请刷新',
        icon: 'close'
      })
      //this.locationCity.Name = '未知'
      setTimeout(() => {
        if (!this.locationInit) {
          this.locationInit = true
          this.listLoad = true
          this.isRefresh = true
          this.onRefresh()
        }
      }, 1000)
    },

    // 精准定位成功
    onComplete(data) {
      console.log('精准定位成功')
      console.log(data,"选择地区")
      localStorage.setItem('GeolocationData', JSON.stringify(data))
      this.$toast.clear()
      //this.closeAreaSelect(false)
      // 对查询条件经纬度赋值
      this.Lng = data.position.lng
      this.Lat = data.position.lat
      //if (!this.locationInit) {
      //  this.locationCity.Name = data.addressComponent.district || data.addressComponent.city
      //  const adcode = parseInt(data.addressComponent.adcode.substr(0, 4) + '00').toString()
      //  this.GetCityIdByCode(adcode)
      //} else {
      //  this.listLoad = true
      //  this.isRefresh = true
      //  this.onRefresh()
      //}
    },
	}
}
</script>
<style lang="scss" scoped>
	.btn {
		width: 170px;
		height: 64px;
		line-height: 64px;
		border-radius: 32px;
		text-align: center;
		margin-right: 24px;
		margin-top: 12px;
	}
	.btn:last-child{
		margin-right: 0;
	}	

</style>
